package com.yh.travel.route.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yh.travel.core.service.impl.CrudServiceImpl;
import com.yh.travel.route.dao.RouteDao;
import com.yh.travel.route.dao.RouteImgDao;
import com.yh.travel.route.pojo.Route;
import com.yh.travel.route.pojo.RouteImg;
import com.yh.travel.route.service.IRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName RouteServiceImpl
 * @Description RouteService层
 * @Author 0715-YuHao
 * @Date 2020/10/31 12:35
 */
@Service
public class RouteServiceImpl extends CrudServiceImpl<RouteDao, Route> implements IRouteService {

    @Autowired
    private RouteImgDao routeImgDao;

    @Override
    @Transactional(readOnly = false)
    public boolean saveOrUpdate(Route entity) {
        boolean result = super.saveOrUpdate(entity); //保存线路
        //根据线路id，删除线路的详细图片列表
        routeImgDao.delete(Wrappers.<RouteImg>lambdaQuery().eq(RouteImg::getRid, entity.getId()));
        //添加线路详细图片列表
        for (int i=0; i<entity.getBigImgList().size(); i++) {
            RouteImg img = new RouteImg();
            img.setRid(entity.getId());
            img.setBigPic(entity.getBigImgList().get(i));
            img.setSmallPic(entity.getSmallImgList().get(i));
            routeImgDao.insert(img);
        }
        return result;
    }
}
